﻿using Jizsoft.JFrame.Modeling.Domain.Entities;
using Jizsoft.JFrame.Modeling.Domain.Services;
using Jizsoft.JFrame.Result;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Http;

namespace Jizsoft.JFrame.Modeling.Web.Api
{
    public class ModelController : ApiController
    {
        [HttpPost]
        public ServiceResult Create(CreatePrameter parameter)
        {
            return new CreateService().CreateModel(parameter.Code, parameter.Name, parameter.Description);
        }

        [HttpGet]
        public PagedResult<Model> UnpublishedList(int page, int limit)
        {
            return new QueryService().QueryUnpublishedModelList(page * limit, limit);
        }

        [HttpGet]
        public PagedResult<Model> PublishedList(int page, int limit)
        {
            return new QueryService().QueryPublishedModelList(page * limit, limit);
        }

        [HttpPost]
        public ServiceResult CreateField(CreateFieldPrameter parameter)
        {
            return new CreateService().CreateModelField(parameter.ModelId, parameter.Name, parameter.FieldType, parameter.IsRequired, parameter.SortIndex, parameter.Description);
        }

        [HttpGet]
        public ListResult<ModelField> FieldList(Guid modelId)
        {
            return new QueryService().QueryModelFieldList(modelId);
        }

        [HttpPost]
        public ServiceResult DeleteField(DeleteFieldParameter parameter)
        {
            return new DeleteService().DeleteModelField(parameter.Id);
        }

        [HttpPost]
        public ServiceResult CreateRelation(CreateRelationParameter parameter)
        {
            return new CreateService().CreateModelRelation(parameter.Model1Id, parameter.Model1Multiplicity, parameter.Model2Id, parameter.Model2Multiplicity, parameter.Type);
        }

        [HttpGet]
        public ListResult<Domain.Views.ModelRelationDetail> RelationList(Guid modelId)
        {
            return new QueryService().QueryModelRelationList(modelId);
        }

        [HttpPost]
        public ServiceResult DeleteRelation(DeleteRelationParameter parameter)
        {
            return new DeleteService().DeleteModelRelation(parameter.Model1Id, parameter.Model2Id);
        }

        [HttpPost]
        public ServiceResult Publish(IdParameter parameter)
        {
            return new PublishService().PublishModel(parameter.Id);
        }
        
        [HttpPost]
        public ServiceResult Branch (IdParameter parameter)
        {

        }

        public class CreateRelationParameter
        {
            public Guid Model1Id { get; set; }
            public int Model1Multiplicity { get; set; }
            public Guid Model2Id { get; set; }
            public int Model2Multiplicity { get; set; }
            public int Type { get; set; }
        }

        public class IdParameter
        {
            public Guid Id { get; set; }
        }

        public class DeleteFieldParameter
        {
            public Guid Id { get; set; }
        }
        
        public class DeleteRelationParameter
        {
            public Guid Model1Id { get; set; }
            public Guid Model2Id { get; set; }
        }


        public class CreateFieldPrameter
        {
            public Guid ModelId { get; set; }
            public string Name { get; set; }
            public Guid FieldType { get; set; }
            public bool IsRequired { get; set; }
            public int SortIndex { get; set; }
            public string Description { get; set; }
        }

        public class CreatePrameter
        {
            public string Code { get; set; }
            public string Name { get; set; }
            public string Description { get; set; }
        }
    }
}
